home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / adt / filename.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  1.9 KB  |  105 lines

  1.  
  2. #include <strings.h>
  3. #include <stdio.h>
  4. #include <pwd.h>
  5.  
  6. #include "utils/log.h"
  7.  
  8. #define MAXPATHLEN 256
  9.  
  10. /*
  11.  * "$Header: /private/postgres/src/utils/adt/RCS/filename.c,v 1.4 1991/11/09 01:48:49 mer Exp $"
  12.  */
  13. char *pg_username ARGS((void ));
  14.  
  15.  
  16. char *
  17. filename_in(file)
  18.  
  19. char *file;
  20.  
  21. {
  22.     char *str, *getenv();
  23.     int ind;
  24.  
  25.     str = (char *) palloc(MAXPATHLEN * sizeof(*str));
  26.     str[0] = '\0';
  27.     if (file[0] == '~') {
  28.     if (file[1] == '\0' || file[1] == '/') {
  29.         /* Home directory */
  30.  
  31.         char name[16], *p;
  32.         struct passwd *pw;
  33.  
  34.         strcpy(name, pg_username());
  35.  
  36.         if ((pw = getpwnam(name)) == NULL) {
  37.         elog(WARN, "User %s is not a Unix user on the db server.",
  38.              name);
  39.         }
  40.  
  41.         strcpy(str, pw->pw_dir);
  42.  
  43.         ind = 1;
  44.     } else {
  45.         /* Someone else's directory */
  46.         char name[16], *p;
  47.         struct passwd *pw;
  48.         int len;
  49.  
  50.         if ((p = (char *) index(file, '/')) == NULL) {
  51.         strcpy(name, file+1);
  52.         len = strlen(name);
  53.         } else {
  54.         len = (p - file) - 1;
  55.         strncpy(name, file+1, len);
  56.         name[len] = '\0';
  57.         }
  58.         /*printf("name: %s\n");*/
  59.         if ((pw = getpwnam(name)) == NULL) {
  60.         elog(WARN, "No such user: %s\n", name);
  61.         ind = 0;
  62.         } else {
  63.         strcpy(str, pw->pw_dir);
  64.         ind = len + 1;
  65.         }
  66.     }
  67.     } else if (file[0] == '$') {  /* $POSTGRESHOME, etc.  expand it. */
  68.     char name[16], environment[80], *envirp, *p;
  69.     int len;
  70.  
  71.     if ((p = (char *) index(file, '/')) == NULL) {
  72.         strcpy(environment, file+1);
  73.         len = strlen(environment);
  74.     } else {
  75.         len = (p - file) - 1;
  76.         strncpy(environment, file+1, len);
  77.         environment[len] = '\0';
  78.     }
  79.     envirp = getenv(environment);
  80.     if (envirp) {
  81.         strcpy(str, envirp);
  82.         ind = len + 1;
  83.     }
  84.     else {
  85.         elog(WARN,"Couldn't find %s in your environment", environment);
  86.     }
  87.     } else {
  88.     ind = 0;
  89.     }
  90.     strcat(str, file+ind);
  91.     return(str);
  92. }
  93.  
  94. char *
  95. filename_out(s)
  96.  
  97. char *s;
  98.  
  99. {
  100.     char *ret = (char *) palloc(strlen(s));
  101.  
  102.     strcpy(ret, s);
  103.     return(ret);
  104. }
  105.